﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain.Service
{
    /// <summary>
    /// 结算服务
    /// </summary>
    public class SettleService
    {
        /// <summary>
        /// 为某一天做日结
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public SingleResult<Guid> DialySettle(Guid businessId, Guid stationId, DateTime date)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                DateTime settledDate = date.Date;
                if (dbContext.Set<Entity.EDailySettlement>().Where(p => p.BusinessId == businessId && p.StationId == stationId && p.SettledDate == settledDate).Select(p => p.Id).Count() > 0)
                {
                    return new SingleResult<Guid>(1, string.Format("{0}已经日结过了，无法继续操作", date.ToString("yyyy/MM/dd")));
                }

                Guid threadId = Sunboxsoft.ASMS.Server.Domain.Threads.DialySettleThread.Start(businessId, stationId, date);

                return new SingleResult<Guid>(threadId);
            }
        }
    }
}
